<?php 
namespace app\admin\controller;

use think\Controller;
use think\Session;
use app\model\Commodity;
use app\model\Picture;
use app\model\Category;

class Commoditydo extends Controller{

	// 发布商品
	public function addCommodity(){
		$pars = array();
		$pars['commodity_id'] = getId();
		$pars['commodity_name'] = input('commodity_name');
		$pars['user_id'] = Session::get("admin_id");
		$pars['commodity_addr'] = input('commodity_addr');
		$pars['commodity_price'] = input('commodity_price');
		$pars['commodity_num'] = input('commodity_num');
		$pars['category_id'] = input('category_id');

		$pictures = explode(",",input('pictures'));
		foreach($pictures as $picture_id){
			$picture = Picture::get($picture_id);
			if($picture!=null){
				$picture->commodity_id = $pars['commodity_id'];
				$picture->save();
			}
		}

		$pars['picture_id'] = $pictures[1];
		$pars['picture_address'] = Picture::get($pars['picture_id'])->picture_address;
		$commodity = Commodity::create($pars);
		$commodity['category_name'] = Category::get($pars['category_id'])->category_name;

		return getResponse(0,$commodity);
	}

	// 查询已审核商品
	public function getAuditedCommodityList(){
		$commoditys = Commodity::getAuditedList(5);
		$this->assign("commoditys",$commoditys);

		return $this->fetch();
	}

	// 查询未审核商品列表
	public function getUnauditedCommodityList(){
		$commoditys = Commodity::getUnauditedList(5);
		$categorys = Category::all(["is_delete"=>0]);
		$this->assign("commoditys",$commoditys);
		$this->assign("categorys",$categorys);

		return $this->fetch();
	}

	// 查询已售出商品
	public function getSoleCommodityList(){
		$commoditys = Commodity::getSoledList(5);
		$this->assign("commoditys",$commoditys);

		return $this->fetch();
	}

	// 查询商品信息
	public function getCommodityInfo(){
		$commodity = Commodity::getInfo(input("commodity_id"));
		return getResponse(0,$commodity);
	}

	// 审核商品
	public function checkCommodity(){
		$commodity = Commodity::get(input("commodity_id"));
		$commodity->is_pass = 1;
		$commodity->admin_id = Session::get("admin_id");
		$commodity->save();

		return getResponse(0,input("commodity_id"));
	}

	// 撤销商品
	public function removeCommodity(){
		$commodity = Commodity::get(input("commodity_id"));
		$commodity->is_pass = 0;
		$commodity->is_delete = 1;
		$commodity->admin_id = Session::get("admin_id");
		$commodity->save();

		return getResponse(0,input("commodity_id"));
	}

	// 查找商品图片
	public function getCommodityPic(){
		$pictures = Picture::all(['commodity_id'=>input("commodity_id")]);

		return getResponse(0,$pictures);
	}


}

 ?>